package com.frontop.system.modules.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.frontop.system.modules.file.model.SysFileGroup;
import com.frontop.system.modules.file.mapper.SysFileGroupMapper;
import com.frontop.system.modules.file.service.SysFileGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YangJia
 * @since 2021-12-13
 */
@Service
public class SysFileGroupServiceImpl extends ServiceImpl<SysFileGroupMapper, SysFileGroup> implements SysFileGroupService {

    @Override
    public List<SysFileGroup> getListMenuVO() {
        QueryWrapper<SysFileGroup> sysMenuQueryWrapper = new QueryWrapper<>();
        sysMenuQueryWrapper.orderByAsc("sort");
        List<SysFileGroup> sysFileGroupList = list(sysMenuQueryWrapper);
        //第一个参数必须是当前最高目录的parentId的值，这里写0也就是一级目录的parentId的值
        return recursionForSysFileGroupManageList(0, sysFileGroupList);
    }


    /**
     * 文件目录树
     * @param parentId
     * @param menuList
     * @return
     */
    private List<SysFileGroup> recursionForSysFileGroupManageList(int parentId, List<SysFileGroup> menuList) {
        List<SysFileGroup> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(sysFileGroup -> sysFileGroup.getParentId().equals(parentId))
                .forEach(sysFileGroup -> {
                    sysFileGroup.setChildren(recursionForSysFileGroupManageList(sysFileGroup.getId(), menuList));
                    list.add(sysFileGroup);
                });
        return list;
    }
}
